From 290f6ab710282bd377f818483a56f480fe8f907c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Mar 2017 14:17:51 +0300 Subject: [PATCH] Don't read ~/.cargo/config in tests Closes #3863 --- src/cargo/core/workspace.rs | 4 +--- src/cargo/util/config.rs | 9 ++------- src/cargo/util/paths.rs | 39 +++++++++++++++++++++++++++++++++++++ tests/cargotest/lib.rs | 1 + 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 1b420e8a4..5015448a5 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -266,8 +266,7 @@ impl<'cfg> Workspace<'cfg> { } } - let mut cur = manifest_path.parent().and_then(|p| p.parent()); - while let Some(path) = cur { + for path in paths::ancestors(manifest_path).skip(2) { let manifest = path.join("Cargo.toml"); debug!("find_root - trying {}", manifest.display()); if manifest.exists() { @@ -286,7 +285,6 @@ impl<'cfg> Workspace<'cfg> { WorkspaceConfig::Member { .. } => {} } } - cur = path.parent(); } Ok(None) diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index a71530663..c0527a760 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -17,6 +17,7 @@ use core::shell::{Verbosity, ColorConfig}; use core::MultiShell; use util::{CargoResult, CargoError, ChainError, Rustc, internal, human}; use util::{Filesystem, LazyCell}; +use util::paths; use util::toml as cargo_toml; @@ -698,10 +699,9 @@ pub fn homedir(cwd: &Path) -> Option { fn walk_tree(pwd: &Path, mut walk: F) -> CargoResult<()> where F: FnMut(File, &Path) -> CargoResult<()> { - let mut current = pwd; let mut stash: HashSet = HashSet::new(); - loop { + for current in paths::ancestors(pwd) { let possible = current.join(".cargo").join("config"); if fs::metadata(&possible).is_ok() { let file = File::open(&possible)?; @@ -710,11 +710,6 @@ fn walk_tree(pwd: &Path, mut walk: F) -> CargoResult<()> stash.insert(possible); } - - match current.parent() { - Some(p) => current = p, - None => break, - } } // Once we're done, also be sure to walk the home directory even if it's not diff --git a/src/cargo/util/paths.rs b/src/cargo/util/paths.rs index d47598a2e..48273ae92 100644 --- a/src/cargo/util/paths.rs +++ b/src/cargo/util/paths.rs @@ -142,3 +142,42 @@ pub fn bytes2path(bytes: &[u8]) -> CargoResult { Err(..) => Err(human("invalid non-unicode path")), } } + +pub fn ancestors(path: &Path) -> PathAncestors { + PathAncestors::new(path) +} + +pub struct PathAncestors<'a> { + current: Option<&'a Path>, + stop_at: Option +} + +impl<'a> PathAncestors<'a> { + fn new(path: &Path) -> PathAncestors { + PathAncestors { + current: Some(path), + //HACK: avoid reading `~/.cargo/config` when testing Cargo itself. + stop_at: env::var("__CARGO_TEST_ROOT").ok().map(PathBuf::from), + } + } +} + +impl<'a> Iterator for PathAncestors<'a> { + type Item = &'a Path; + + fn next(&mut self) -> Option<&'a Path> { + if let Some(path) = self.current { + self.current = path.parent(); + + if let Some(ref stop_at) = self.stop_at { + if path == stop_at { + self.current = None; + } + } + + Some(path) + } else { + None + } + } +} diff --git a/tests/cargotest/lib.rs b/tests/cargotest/lib.rs index d6a9be7a7..1b5899b7f 100644 --- a/tests/cargotest/lib.rs +++ b/tests/cargotest/lib.rs @@ -50,6 +50,7 @@ fn _process(t: &OsStr) -> cargo::util::ProcessBuilder { .env_remove("CARGO_HOME") .env("HOME", support::paths::home()) .env("CARGO_HOME", support::paths::home().join(".cargo")) + .env("__CARGO_TEST_ROOT", support::paths::root()) .env_remove("RUSTC") .env_remove("RUSTFLAGS") .env_remove("CARGO_INCREMENTAL") -- 2.30.2